# -*- encoding:utf8 -*-

import MySQLdb
import re

db = MySQLdb.connect("localhost","root","zsx123456","python_db",charset='utf8')
file = open("data.sql")

cursor = db.cursor()
try:
    while 1:
        line = file.readline()
        if not line:
            break
        else:
            print "待执行的sql:",line
            try:
                cursor.execute(line)
                db.commit()
            except Exception,reason:
                print "执行失败！，失败原因:",reason
                db.rollback()
except Exception,e:
    print "读取文件失败，失败原因:",e
finally:
    if file:
        print "关闭文件"
        file.close()
    if db:
        print "关闭数据库连接"
        db.close()



